package test;

import common.Utils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * Created with IntelliJ IDEA
 * Descroption
 * User:dell
 * Date:2025-09-09
 * Time:9:32
 */
public class MessagePostPage extends Utils {
    public static String url = "http://81.70.248.108:58080/index.html";
    public MessagePostPage() {
        super(url);
    }
    /**
     *检查元素
     */
    public void check(){
        //获取帖子上的用户名
        String author = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[2]/div/div[1]/ul/li[1]")).getText();
        String articleTitle = webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).getText();
        if (author.equals("测试用户1")){
            return;
        }
        //页面的跳转
        webDriver.findElement(By.xpath("//*[@id=\"artical-items-body\"]/div[1]/div/div[2]/div[1]/a/strong")).click();

        //等待页面跳转
        WebDriverWait detailWait = new WebDriverWait(webDriver, Duration.ofSeconds(10));
        detailWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"details_article_title\"]")));
        //获取元素
        String jump_author = webDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).getText();
        String jump_articleTitle = webDriver.findElement(By.xpath("//*[@id=\"details_article_title\"]")).getText();

        assert author.equals(jump_author):"作者名称不对应";
        assert articleTitle.equals(jump_articleTitle):"帖子标题不对应";
    }
    /**
     * 在详情页给作者发私信
     */
    public void detailPostMes(){
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        detailWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btn_details_send_message\"]")));

        WebElement postMessageEle = webDriver.findElement(By.xpath("//*[@id=\"btn_details_send_message\"]"));
        postMessageEle.click();

        detailWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"index_message_modal\"]/div/div/div[1]/h5")));
        String boardTitle = webDriver.findElement(By.xpath("//*[@id=\"index_message_modal\"]/div/div/div[1]/h5")).getText();
        assert boardTitle.equals("发送站内信"):"跳转错误";

        //1.取消发送
        detailWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"index_message_receive_cancel\"]")));
        WebElement cancelEle = webDriver.findElement(By.xpath("//*[@id=\"index_message_receive_cancel\"]"));
        cancelEle.click();

        //2. 确定发送
        WebElement postMessageEle2 = webDriver.findElement(By.xpath("//*[@id=\"btn_details_send_message\"]"));
        postMessageEle2.click();


        //编辑信息
        detailWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"index_message_receive_content\"]")));
        WebElement contentEle = webDriver.findElement(By.xpath("//*[@id=\"index_message_receive_content\"]"));
        contentEle.click();
        contentEle.sendKeys("你好!");

        //点击确定
        detailWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btn_index_send_message\"]")));
        WebElement postEle = webDriver.findElement(By.xpath("//*[@id=\"btn_index_send_message\"]"));
        postEle.click();


    }

    /**
     * 进入作责主页发私信
     */
    public void homePostMes(){
        WebDriverWait detailWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        detailWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"article_details_author_name\"]")));

        //查看作者昵称是否一致
        String authorNicknameBefore = webDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).getText();
        webDriver.findElement(By.xpath("//*[@id=\"article_details_author_name\"]")).click();

        //等待页面跳转
        WebDriverWait homeWait = new WebDriverWait(webDriver,Duration.ofSeconds(10));
        homeWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"profile_nickname\"]")));
        String authorNicknameAfter = webDriver.findElement(By.xpath("//*[@id=\"profile_nickname\"]")).getText();
        assert authorNicknameAfter.equals(authorNicknameBefore):"跳转失败";

        //发送私信
        homeWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btn_profile_send_message\"]")));
        WebElement messagePost = webDriver.findElement(By.xpath("//*[@id=\"btn_profile_send_message\"]"));
        messagePost.click();

        //1.取消发送
        homeWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"index_message_receive_cancel\"]")));
        webDriver.findElement(By.xpath("//*[@id=\"index_message_receive_cancel\"]")).click();

        //2.确认发送
        homeWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"btn_profile_send_message\"]")));
        WebElement messagePost2 = webDriver.findElement(By.xpath("//*[@id=\"btn_profile_send_message\"]"));
        messagePost2.click();

        homeWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"index_message_receive_content\"]")));
        WebElement contentEle = webDriver.findElement(By.xpath("//*[@id=\"index_message_receive_content\"]"));
        contentEle.click();
        contentEle.sendKeys("你好呀！");

        //点击发送
        webDriver.findElement(By.xpath("//*[@id=\"btn_index_send_message\"]")).click();

        //*[@id="index_message_receive_cancel"]
        //*[@id="btn_profile_send_message"]



    }
}

